package com.ml.mall.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.page.Page;
import com.ml.mall.bean.req.sys.SysRolePermissionDto;
import com.ml.mall.bean.req.sys.SysSubRoutingDelDto;
import com.ml.mall.bean.req.sys.SysSubRoutingNewDto;
import com.ml.mall.bean.req.sys.SysSubRoutingQueryPageDto;
import com.ml.mall.entity.sys.SysRouting;
import com.ml.mall.entity.sys.SysSubscription;
import com.ml.mall.entity.sys.SysSubscriptionRouting;
import com.ml.mall.service.sys.SysRoutingService;
import com.ml.mall.service.sys.SysSubscriptionRoutingService;
import com.ml.mall.mapper.sys.SysSubscriptionRoutingMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SysSubscriptionRoutingServiceImpl extends ServiceImpl<SysSubscriptionRoutingMapper, SysSubscriptionRouting>
    implements SysSubscriptionRoutingService{

    @Autowired
    private SysSubscriptionRoutingMapper sysSubscriptionRoutingMapper;

    @Autowired
    private SysRoutingService sysRoutingService;

    /**
     * 分页查询配套路由信息
     * @param dto
     * @return
     */
    @Override
    public Page<SysRouting> subRoutingQueryPage(SysSubRoutingQueryPageDto dto) {
        Page<SysRouting> pageResult = new Page<>(dto.getPageIndex(), dto.getPageNum());

        Integer count = sysSubscriptionRoutingMapper.selectPageCount(dto);
        pageResult.setTotal(count);
        List<SysRouting> sysRoutings = sysSubscriptionRoutingMapper.selectPage(dto);
        pageResult.setRecords(sysRoutings);
        return pageResult;
    }

    /**
     * 添加配套路由信息
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Integer subRoutingNew(SysSubRoutingNewDto dto) {
        //删除已有配套路由信息
        LambdaQueryWrapper<SysSubscriptionRouting> deleteWrapper = new LambdaQueryWrapper<>();
        deleteWrapper.eq(SysSubscriptionRouting::getSubId, dto.getSubId());
        remove(deleteWrapper);

        //添加新的配套信息
        List<Long> routingIds = dto.getRoutingIds();
        if(routingIds != null && routingIds.size() > 0){
            return sysSubscriptionRoutingMapper.insertBatch(dto);
        }
        return -1;
    }

    /**
     * 删除配套路由信息
     * @param dto
     * @return
     */
    @Override
    public Integer subRoutingDel(SysSubRoutingDelDto dto) {
        LambdaQueryWrapper<SysSubscriptionRouting> delMapper = new LambdaQueryWrapper<>();
        delMapper.eq(SysSubscriptionRouting::getSubId, dto.getSubId());
        delMapper.in(SysSubscriptionRouting::getRoutingId, dto.getRoutingIds());

        return sysSubscriptionRoutingMapper.delete(delMapper);
    }
}




